package com.fzj.example.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * ClassName: Handler
 * date: 2019/10/23 14:24
 * Description: TODO
 * @author fanzj@bonree.com
 * @since JDK 1.8
 */
public class Handler implements InvocationHandler {

    //被代理对象
    private Object targetObject;

    //创建一个代理对象
    public Object newProxyIntance(Object targetObject){
        this.targetObject=targetObject;
        return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),targetObject.getClass().getInterfaces(),this);
    }


    //代理对象执行动作
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("代理执行之前");
        Object obj=method.invoke(this.targetObject,args);
        System.out.println("代理执行之后");
        return  obj;
    }
}
